package Class_Object.接口.克隆;

import java.util.Objects;

//class Money {
//    public double m =19.9;//浅拷贝,对象里面有一个对象,但是实例共用一个对象
//}
class Money implements Cloneable{
    public double m =19.9;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable{//要克隆的话一定要实现这个接口,表面当前类是可以被克隆的
    public String name;
    public int age;
    Money money = new Money();


    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //这里在克隆person,但是没有克隆money
       Person tmp = (Person)super.clone();
        tmp.money =(Money) this.money.clone();//为这个对象的money克隆一份
        return tmp;//这才是深拷贝
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
    //
//    @Override
//    public boolean equals(Object obj) {
//        Person tm = (Person) obj;
//        return this.name.equals(tm.name)
//                && this.age == tm.age;
//    }
}
public class Test {
    //TODO Object类
    public static void main(String[] args) {
      Person person1 = new Person("张三",100);
      Person person2 = new Person("张三",100);
        System.out.println(person1.equals(person2));

    }
    public static void main1(String[] args) throws CloneNotSupportedException {
        //TODO 克隆
        Person person = new Person("小白",120);
        Person person1 = (Person) person.clone();//向下转型
        System.out.println("person1 "+person1.money.m);
        System.out.println("person "+person.money.m);
        System.out.println("========");
        person.money.m = 99.9;
        System.out.println("person1 "+person1.money.m);
        System.out.println("person "+person.money.m);

    }


}
